package com.to8to.kitt;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class ThriftBizServerChannelInitializer extends ChannelInitializer<Channel>{

	private ChannelHandlerAdapter bizHandler;
	 
	private boolean log = true;
	
	public boolean isLog() {
		return log;
	}

	public void setLog(boolean log) {
		this.log = log;
	}

	public ThriftBizServerChannelInitializer(ChannelHandlerAdapter bizHandler)
	{
		this.bizHandler = bizHandler;
	}
	
	@Override
	protected void initChannel(Channel ch) throws Exception {
        ChannelPipeline p = ch.pipeline();
        if(log)
        	p.addLast(new LoggingHandler(LogLevel.TRACE));
        p.addLast(new ThriftLengthFieldBasedFrameDecoder());
        p.addLast(new LengthFieldPrepender(4));
        p.addLast(new ThriftMessageCodec());
        p.addLast(bizHandler);
	}

}
